package net.asfun.jvalog.frame;

import java.util.logging.Level;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.asfun.jvalog.mvc.HandlerExceptionResolver;
import net.asfun.jvalog.mvc.ModelAndView;
import net.asfun.jvalog.util.RequestUtil;
import static net.asfun.jvalog.common.log.logger;


public class SimpleExceptionResolver implements HandlerExceptionResolver{

	@Override
	public ModelAndView resolveException(HttpServletRequest req,
			HttpServletResponse resp, Object controller, Exception e) {
		logger.log(Level.WARNING, e.getMessage(), e.getCause());
		if ( RequestUtil.isAjaxRequest(req) ) {
			ModelAndView mav = new JsonableView("json");
			mav.addObject("type", "error");
			mav.addObject("message", e.getMessage());
			return mav;
		} else {
			ModelAndView mav = new JsonableView("tip.html");
			mav.addObject("message", e.getMessage());
			mav.addObject("forward", "javascript:history.go(-1)");
			return mav;
		}
	}

}
